package coder.nav.schedule.action;

import coder.nav.enums.NavIdeLangEnums;
import coder.nav.model.bo.NavIdeRunBO;
import coder.nav.model.bo.NavTaskActionBO;
import coder.nav.model.vo.NavIdeRunOutErrVO;
import coder.nav.service.NavIdeService;
import coder.nav.service.NavMailService;
import coder.nav.service.NavSettingService;
import coder.nav.service.NavTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
public class NavTaskPhpScriptAction extends NavTaskNoticeAction {

    @Autowired
    protected NavSettingService settingService;
    @Autowired
    protected NavTaskService taskService;
    @Autowired
    protected NavMailService mailService;
    @Autowired
    protected NavIdeService ideService;
    @Autowired
    protected RedisTemplate<String, String> redisTemplate;

    @Override
    public String title() {
        return "脚本执行 - PHP";
    }

    @Override
    public String context() {
        return super.context();
    }

    @Override
    public String params() {
        return super.params();
    }

    @Override
    public Date executeTime() {
        return super.executeTime();
    }

    @Override
    public NavTaskActionBO getEntity(Long userId) {
        return super.getEntity(userId);
    }

    @Override
    public void addTask(Long userId) {
        NavTaskActionBO entity = this.getEntity(userId);
        taskService.saveTaskAction(entity);
    }

    @Override
    public void actionTask(NavTaskActionBO task) {
        NavIdeRunBO ideRunBO = new NavIdeRunBO();
        ideRunBO.setLang(NavIdeLangEnums.PHP.getKey());
        ideRunBO.setCode(task.getContext());
        NavIdeRunOutErrVO ideRunOutErrVO = ideService.run(ideRunBO, task.getUserId());
        task.setContext(ideRunOutErrVO.getContext());
        super.actionTask(task);
    }

}
